package cn.jvtc.course.management.system.utils;

import cn.jvtc.course.management.system.bean.*;
import cn.jvtc.course.management.system.dao.impl.*;

import java.io.*;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

/**
 * 将爬取的数据写入数据库
 *
 * @author GaoFeng2017
 * @date 2017-12-09 13:39:52
 **/

public class DataWriter {
    private static List<String> names = new LinkedList<>();
    private static String current_name;
    private static long tid;
    private static long tsid;
    private static List<TeacherLessonDO> teacherLessonDOS =  new LinkedList<>();
    private static byte weekday;
    private static byte weekType;
    private static TeacherLessonDaoImpl teacherLessonDao = new TeacherLessonDaoImpl();
    private static TeacherLessonClassNexusDaoImpl teacherLessonClassNexusDao = new TeacherLessonClassNexusDaoImpl();
    private static TeacherLessonWeekDaoImpl teacherLessonWeekDao = new TeacherLessonWeekDaoImpl();

    public static void main(String[] args) throws IOException {
        File file = new File("h:\\table_class");

        File[] files = file.listFiles();

        Properties pro = new Properties();

        TeacherDaoImpl teacherDao = new TeacherDaoImpl();
        for(File f : files) {
            current_name = f.getName().replace(".properties","");
//            if(current_name.contains("江")) {
                System.out.println("=====================  " + f.getName().replace(".properties", "") + "  ======================");
                pro.load(new InputStreamReader(new FileInputStream(file + "\\" + f.getName())));
                tid = teacherDao.getId(current_name,pro.getProperty("部门"));
                /*TeacherInfoDO teacherInfoDO = new TeacherInfoDO();
                teacherInfoDO.setDepartment(pro.getProperty("部门"));
                teacherInfoDO.setName(current_name);
                teacherInfoDO.setSex(pro.getProperty("性别"));
                teacherInfoDO.setNumber(1000000+Integer.parseInt(pro.getProperty("tid")));
                teacherInfoDO.setPassword(1000000+Integer.parseInt(pro.getProperty("tid")) + "");
                teacherInfoDO.setPermissionCode((byte)2);
                teacherDao.addTeacher(teacherInfoDO);*/


                System.out.println(pro.getProperty("部门"));
                System.out.println(pro.getProperty("性别"));
                System.out.println(pro.getProperty("tid"));
                weekday = 1;
                spliterItem(pro.getProperty("星期一"));
                System.out.println("-------------------------");
                weekday = 2;
                spliterItem(pro.getProperty("星期二"));
                System.out.println("-------------------------");
                weekday = 3;
                spliterItem(pro.getProperty("星期三"));
                System.out.println("-------------------------");
                weekday = 4;
                spliterItem(pro.getProperty("星期四"));
                System.out.println("-------------------------");
                weekday = 5;
                spliterItem(pro.getProperty("星期五"));
                System.out.println("-------------------------");
                weekday = 6;
                spliterItem(pro.getProperty("星期六"));
                System.out.println("-------------------------");
                weekday = 7;
                spliterItem(pro.getProperty("星期日"));
                System.out.println("-------------------------");
                System.out.println(names);
                System.out.println("==========================");
            }
//        }
    }
    private static void spliterItem(String str) {
        String[] result = str.replaceAll("[\\[\\]节]{1,2}","").split(" ,");
            /*分割子项*/
        for(String rs : result) {
            if(!"".equals(rs.trim())) {
//                System.out.println(rs);

                String[] sub = rs.replaceAll("Linux C程序设计","Linux/C程序设计")
                        .replaceAll(" 周","周")
                        .replaceAll(" 单","单")
                        .replaceAll(" 双","双")
                        .replaceAll("([1-9]\\d{3})","$1,")
                        .replaceAll(", ",",")
                        .replaceAll("(人数：\\d+)","")
                        .replaceAll("  "," ").split(" ");

//                System.out.println(Arrays.toString(sub));
                /*分割子项中的课程信息*/
                /*课程名,周次,节次,校区,教室,班级,人数*/
                //特殊课程,同节次,但周次类型不同的课程
                if(sub.length > 7) {
                    for(int j = 4;j < sub.length;j++) {
                        if(sub[j].contains("校区")) {
                            String[] arr1 = Arrays.copyOfRange(sub,j-3,sub.length);
                            String[] arr2 = Arrays.copyOfRange(sub,0,j-2);
                            String[] center = arr1[0].split(",");
                            arr1[0] = center[center.length - 1];
                            arr2[arr2.length - 1] = arr2[arr2.length - 1].replace(arr1[0],"");
                            getSubInfo(arr2);
                            System.out.println(".................................");
                            getSubInfo(arr1);
                        }
                    }
                } else {
                    getSubInfo(sub);
                }
                System.out.println();
            }
        }
//        System.out.println(Arrays.toString(result));
    }
    private static void getSubInfo(String[] sub) {
        if(sub.length < 4) {
            System.out.println("课程信息缺失严重");
            return;
        }
        TeacherLessonDO teacherLessonDO = new TeacherLessonDO();
        teacherLessonDO.setTeacherId(tid);
        teacherLessonDO.setYearTermId(1);
        //对于一些无校区的,角标特殊处理
        int i = 0;
        //课程名
        System.out.println(sub[0]);
        teacherLessonDO.setName(sub[0]);
        //周次
        if(sub[1].contains("单周")) {
            teacherLessonDO.setWeekType((byte) 0);
        } else if(sub[1].contains("双周")) {
            teacherLessonDO.setWeekType((byte) 1);
        } else {
            teacherLessonDO.setWeekType((byte) 2);
        }


        //节次
        String[] times = sub[2].split("-");
        System.out.println(times[0] + "-" + times[1]);
        teacherLessonDO.setPeriod(times[0] + "-" + times[1]);
        //校区
        if(sub[3].contains("校区")) {
            if(sub[3].contains("十里")) {
                System.out.println(0);
                teacherLessonDO.setCampus((byte) 0);
            } else {
                System.out.println(1);
                teacherLessonDO.setCampus((byte) 1);

            }
        } else {
            System.out.println("混合校区" + 2);
            teacherLessonDO.setCampus((byte) 2);
            i++;
        }

        teacherLessonDO.setWeekday(weekday);

        //教室
        if(sub[0].contains("体育")) {
            System.out.println("操场(待定)");
            teacherLessonDO.setClassroom("操场(待定)");
        } else {
            if(sub.length > 6) {
                System.out.println(sub[4-i] + "(" + sub[5-i] + ")");
                teacherLessonDO.setClassroom(sub[4-i] + "(" + sub[5-i] + ")");

            } else {
                System.out.println(sub[4-i]);
                teacherLessonDO.setClassroom(sub[4-i]);
            }
        }

        //添加
//        TeacherLessonDaoImpl teacherLessonDao = new TeacherLessonDaoImpl();
//        teacherLessonDao.addTeacherLesson(teacherLessonDO);
//        teacherLessonDOS.add(teacherLessonDO);

        long lid = teacherLessonDao.getId(teacherLessonDO);
        if(lid == 0) {
            System.exit(1);
        }
        for(String s : sub[1].replaceAll("[单双周]","").replaceAll(" ","").split(",")) {
            TeacherLessonWeekDO teacherLessonWeekDO = new TeacherLessonWeekDO();
            teacherLessonWeekDO.setTeacherLessonId(lid);
            if (!s.contains("-")) {
                System.out.print(s + "-" + s + ",");
                teacherLessonWeekDO.setBeginWeek(Byte.parseByte(s));
                teacherLessonWeekDO.setEndWeek(Byte.parseByte(s));
            } else {
                String[] weeks = s.split("-");
                teacherLessonWeekDO.setBeginWeek(Byte.parseByte(weeks[0]));
                teacherLessonWeekDO.setEndWeek(Byte.parseByte(weeks[1]));
                System.out.print(weeks[0] + "-" + weeks[1] + ",");
            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            teacherLessonWeekDao.addTeacherLessonWeek(teacherLessonWeekDO);
        }

        System.out.println();

        ClassDaoImpl dao = new ClassDaoImpl();
        //班级
        String[] classes = sub[sub.length - 1].split(",");

        //特殊班级,班级带字母
        if(classes[classes.length - 1].length() <= 1) {
            classes[classes.length - 2] = classes[classes.length - 2] + classes[classes.length - 1];
            classes = Arrays.copyOfRange(classes,0,classes.length - 1);
        }

        for(String cs : classes) {
            cs = cs.replaceAll("沐.+装","沐昇装").replaceAll("沐.+室","沐昇室");
            System.out.print(cs + "+");
            TeacherClassNexusDaoImpl teacherClassNexusDao = new TeacherClassNexusDaoImpl();
            long cid = dao.getId(cs);
//            System.out.println(cs);

            if(cid == 0) {
                System.exit(1);
            }

            /*long lid = teacherLessonDao.getId(teacherLessonDO);
            if(lid == 0) {
                System.exit(1);
            }*/

            /*TeacherLessonClassNexusDO teacherLessonClassNexusDO = new TeacherLessonClassNexusDO();
            teacherLessonClassNexusDO.setClassId(cid);
            teacherLessonClassNexusDO.setTeacherLessonId(lid);*/

//            teacherLessonClassNexusDao.addTeacherLessonClassNexus(teacherLessonClassNexusDO);


            /*if(!teacherClassNexusDao.isNexusExist(tid,cid)) {
                TeacherClassNexusDO teacherClassNexusDO = new TeacherClassNexusDO();
                teacherClassNexusDO.setClassId(cid);
                teacherClassNexusDO.setTeacherId(tid);
                teacherClassNexusDao.addTeacherClassNexus(teacherClassNexusDO);
            }*/


            /*ClassInfoDO classInfoDO = new ClassInfoDO();
            classInfoDO.setName(cs);
            classInfoDO.setGrade(Short.parseShort(20 + cs.substring(cs.length() - 4,cs.length() - 2)));
            if(!dao.isClassExist(cs)) {
                dao.addClass(classInfoDO);
            }*/


        }
    }
}
